
package com.dict;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;


public class XMLHandler extends DefaultHandler {
	Boolean curElement = false;
	String curValue = null;
	public static XMLList list = null;

	public static XMLList getXMLList() {
		return list;
	}

	public static void setXMLList(XMLList list) {
		XMLHandler.list = list;
	}


	@Override
	public void startElement(String uri, String tagName, String qName,
			Attributes attributes) throws SAXException {

		curElement = true;
		String att;
		int i = 0;
		if (tagName.equals("dictionary"))
		{
			/** Start */
			list = new XMLList();
			att=attributes.getValue("totalresults");
			i=Integer.parseInt(att);
		} else if(tagName.equals("partofspeech")){
			/** Get attribute value */
			String attr = attributes.getValue("pos");
			Log.d("dic", attr);
			list.setPartsOfSpeech(attr);
		}
		
	
		if(tagName.equals("dictionary")&& i==0){
			list.setResult("No results found");
		}
	}




	@Override
	public void endElement(String uri, String tagName, String qName)
			throws SAXException {

		curElement = false;

		/** set value */
		
		if (tagName.equalsIgnoreCase("def"))
			list.setDef(curValue);
		else if (tagName.equalsIgnoreCase("pron"))
			list.setPron(curValue);
		else if(tagName.equalsIgnoreCase("display_form"))
			list.setDefSize(curValue);
	}


	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (curElement) {
			curValue = new String(ch, start, length);
			curElement = false;
		}

	}


}
